<?php
/**
 * Created by YuanXiShop.
 * User: YuanXi
 * Date: 2021/6/3 0003
 * Time: 1:26
 */

namespace admin;

use JasonGrimes\Paginator;
use models\BaseDao;
use Slince\Upload\UploadHandlerBuilder;

class Article extends Admin
{
    function __construct()
    {
        parent::__construct();
        $this->assign('menumark','article');
        //全部分类
        $db = new BaseDao();
        $data = $db->select('class','*',['ORDER'=>['ord'=>'ASC','id'=>'DESC']]);
        $this->assign('aclass',$data);
    }
    function index()
    {
        $db = new BaseDao();

        $num = $_GET['num'] ?? 1;
        //搜索条件
        $prosql['ORDER'] = ['id'=>'DESC'];
        $where = [];
        if(!empty($_GET['cid']) || $_GET['cid'] != 0){
            $where['cid'] = $_GET['cid'];
            $cid = '&cid='.$_GET['cid'];
        }
        if(!empty($_GET['name'])){
            $where['name[~]'] = $_GET['name'];
            $name = '&name='.$_GET['name'];
        }
        $this->assign('get',$_GET);

        $totalItems = $db->count('article',$where);//总页面数
        $itemsPerPage = PNUM;//每页显示多少条数据
        $currentPage = $num;//当前页是第几页
        $urlPattern = '/admin/article?num=(:num)'.$cid.$name;

        $paginator = new Paginator($totalItems, $itemsPerPage, $currentPage, $urlPattern);

        //从第几页开始
        $start = ($currentPage-1) * $itemsPerPage;
        $prosql['LIMIT'] = [$start,$itemsPerPage];

        $prosql = array_merge($prosql,$where);
        $data = $db->select('article','*',$prosql);

        //将数据分配给模板
        $this->assign('data',$data);
        $this->assign('fpage',$paginator);

        //标题
        $this->assign('title','文章列表');
        //输出模板
        $this->display('article/index');
    }
    function add()
    {
        if($_POST['do_submit']){
            $db = new BaseDao();
            unset($_POST['do_submit']);
            $_POST['atime'] = empty($_POST['atime']) ? time() : strtotime($_POST['atime']);
            if($db->insert('article',$_POST)){
                $this->success('/admin/article','添加成功！');
            }else{
                $this->error('/admin/article','添加失败！');
            }
        }
        $this->assign('title','文章添加');
        $this->display('article/add');
    }
    function mod($id)
    {
        $db = new BaseDao();
        $data = $db->get('article','*',['id'=>$id]);
        $this->assign($data);
        $this->assign('title','修改文章');
        $this->display('article/mod');
    }
    function doupdate()
    {
        $id = $_POST['id'];
        unset($_POST['id']);
        $db = new BaseDao();
        $_POST['atime'] = empty($_POST['atime']) ? time() : strtotime($_POST['atime']);
        if($db->update('article',$_POST,['id'=>$id])){
            $this->success('/admin/article','修改成功！');
        }else{
            $this->error('/admin/article/mod/'.$id,'修改失败！');
        }
    }
    function del($id)
    {
        $db = new BaseDao();
        if($db->delete('article',['id'=>$id])){
            $this->success('/admin/article','删除成功！');
        }else{
            $this->error('/admin/article','删除失败！');
        }
    }
    function alldel()
    {
        $db = new BaseDao();
        $num = 0;
        foreach ($_POST['id'] as $id){
            $num += $db->delete('article',['id'=>$id]);
        }
        if($num > 0){
            $this->success('/admin/article',$num.'篇删除成功！');
        }else{
            $this->error('/admin/article','删除失败！');
        }
    }

    function upload()
    {
        $path = TEMPDIR."/uploads/article";

        $builder = new UploadHandlerBuilder(); //create a builder.
        $handler = $builder

            //add constraints

            ->allowExtensions(['jpg', 'png', 'gif'])
            ->allowMimeTypes(['image/*'])

            ->saveTo($path) //save to local
            ->getHandler();


        $files = $handler->handle();
        $filename = $files['file']->getUploadedFile()->getClientOriginalName();

        $newfilename =  date('Y-md') . '-' . uniqid() . '.' . $files['file']->getUploadedFile()->getClientOriginalExtension();

        rename($path.'/'.$filename, $path.'/'.$newfilename);


        $url = getCurUrl();

        $arr['src'] = $url."/uploads/article/".$newfilename;

        echo json_encode($arr);
    }
}